﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace DR5.SPExtend
{
    public static class PageUtility
    {
        public static void AddStylesheet(this Page page, string url)
        {
            string link = String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", url);
            page.Header.Controls.Add(new LiteralControl { Text = link });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="con">Control where script tag will be added</param>
        /// <param name="txt">text which shown in alert msg box</param>
        public static void SetAlertOnPageLoad(Control con, string txt)
        {
            con.Controls.Add(new LiteralControl("<script>alert('" + txt + "')</script>"));
        }

        public static void SetAlertOnPageLoad(Page con, string txt)
        {
            con.Controls.Add(new LiteralControl("<script>alert('" + txt + "')</script>"));
        }

        public static string AddQueryString(Uri currentUrl, string queryKey, string queryValue)
        {
            //string queryKey = this.Field.InternalName;
            string redirecturl = string.Empty;
            if (currentUrl.Query.Length == 0)
            {
                redirecturl = currentUrl + "?" + queryKey + "=" + queryValue;
            }
            else
            {
                if (currentUrl.Query.Contains(queryKey))
                {
                    Regex.Replace(currentUrl.ToString(), @"([?&]" + queryKey + ")=[^?&]+", "$1=" + queryValue);
                }
                else
                {
                    redirecturl = currentUrl + "&" + queryKey + "=" + queryValue;
                }
            }
            return redirecturl;
        }

        public static string MapPathReverse(this Page page,string fullServerPath)
        {
            return fullServerPath.Replace(page.Request.PhysicalApplicationPath, String.Empty);
        }
    }
}
